package com.daxidi.cn.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.daxidi.cn.entity.Clerk;
import com.daxidi.cn.entity.Manager;
import com.daxidi.cn.entity.Shop;
import com.daxidi.cn.mapper.ClerkMapper;
import com.daxidi.cn.mapper.ShopMapper;
import com.daxidi.cn.service.IClerkService;
import com.daxidi.cn.vo.ClerkVo;
import com.daxidi.cn.vo.ShopClerkVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商家店员信息表 服务实现类
 * </p>
 *
 * @author wsh
 * @since 2020-11-09
 */
@Service
public class ClerkServiceImpl extends ServiceImpl<ClerkMapper, Clerk> implements IClerkService {
    @Autowired
    private ClerkMapper clerkMapper;

    @Autowired
    private ShopMapper shopMapper;

    // 添加店员
    @Override
    public void addClerk(Clerk clerk) {
        clerkMapper.insert(clerk);
    }

    // 根据店员id删除店员
    @Override
    public void deleteClerkById(Integer id) {
        clerkMapper.deleteById(id);
    }

    // 根据店员id修改店员
    @Override
    public void updateClerkById(Clerk clerk) {
        clerkMapper.updateById(clerk);
    }

    // 根据商店id查询该商店所有店员
    @Override
    public Map selectClerksPage(Integer pageNum, Integer pageSize, Integer shopId, String name, String mobile, Integer enable) {
        Page<Clerk> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Clerk> wrapper = new QueryWrapper<>();
        if (null != shopId && !"".equals(shopId)){
            wrapper.eq("shop_id", shopId);
        }
        if (null != name && !"".equals(name)){
            wrapper.like("name", name);
        }
        if (null != mobile && !"".equals(mobile)){
            wrapper.like("moblie", mobile);
        }
        if (null != enable && !"".equals(enable)){
            wrapper.eq("enable", enable == 1 ? true : false);
        }
        Page<Clerk> clerkPage = clerkMapper.selectPage(page, wrapper);
        List<Clerk> clerks = clerkPage.getRecords();
        List<ShopClerkVo> shopClerkVos = new ArrayList<>();
        clerks.forEach(clerk -> {
            ShopClerkVo shopClerkVo = new ShopClerkVo();
            BeanUtils.copyProperties(clerk, shopClerkVo);
            QueryWrapper<Shop> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("id", clerk.getShopId());
            shopClerkVo.setShopName(shopMapper.selectOne(wrapper1).getName());
            shopClerkVo.setRole(clerk.getRole());
            shopClerkVos.add(shopClerkVo);
        });
        long total = clerkPage.getTotal();
        Map map = new HashMap();
        map.put("data", shopClerkVos);
        map.put("count", total);
        return map;
    }

    // 根据店员id查询单个店员
    @Override
    public Clerk selectClerkById(Long id) {
        QueryWrapper<Clerk> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Clerk clerk = clerkMapper.selectOne(wrapper);
        return clerk;
    }

    // 店员登录方法
    @Override
    public Clerk login(String mobile, String password) {
        QueryWrapper<Clerk> wrapper = new QueryWrapper<>();
        wrapper.eq("moblie", mobile);
        wrapper.eq("password", password);
        Clerk clerk = clerkMapper.selectOne(wrapper);
        return clerk;
    }

    @Override
    public List<ClerkVo> showClerkByShopId(Long shopId, Integer page, Integer limit) {
        List<ClerkVo> clerkVos = clerkMapper.showClerksByShopId(shopId, page, limit);
        return clerkVos;
    }
}
